#include <ClassDataBaseWindow.h>
#include <iostream>
#include <EDesignerCore.h>
#include <ClassDataBase.h>
#include <QTreeWidgetItem>
#include <QStringList>
ClassDataBaseWindow::ClassDataBaseWindow( QWidget *parent /*= 0*/ ): WindowBase(parent)
{

    setWindowTitle("Class Data Base");
    getWindowShowHideAction()->setText("Class Data Base");
    getWindowShowHideAction()->setChecked(true);
    getDockWidget()->setWindowTitle("Class Data Base");

    m_treeWidget = new QTreeWidget;
    setCentralWidget(m_treeWidget);
    connect(getWindowShowHideAction(), SIGNAL(toggled (bool)), this, SLOT(windowShowSlot(bool)));


}

ClassDataBaseWindow::~ClassDataBaseWindow()
{

}

Qt::DockWidgetArea ClassDataBaseWindow::dockWidgetArea()
{
    return Qt::DockWidgetArea::LeftDockWidgetArea;
    //hide();
}

void ClassDataBaseWindow::setUpInitialShowHide()
{
    getWindowShowHideAction()->setChecked(false);
}

void ClassDataBaseWindow::refreshTreeWidget()
{
    //std::cout<<"refreshTreeWidget"<<std::endl;
    m_treeWidget->clear();
    QList<QSharedPointer<ClassData>> classDataList = EDesignerCore::instance()->classDataBase()->getClassDataListAll();
    for(int i=0; i < classDataList.size(); i++)
    {
        if(classDataList.at(i)->getShowInClassList())
        {
            QString class_type_name = classDataList.at(i)->getClassTypeName();
            QTreeWidgetItem* item_ptr = new QTreeWidgetItem(QStringList()<<class_type_name);
            m_treeWidget->addTopLevelItem(item_ptr);
        }

    }
}

QString ClassDataBaseWindow::currentClassType()
{
    QString text("-1");//-1代表当前没有选中的item
    QTreeWidgetItem* item_ptr = m_treeWidget->currentItem();
    if(item_ptr != NULL)
    {
        text = item_ptr->text(0);
    }
    return text;
}

void ClassDataBaseWindow::windowShowSlot(bool checked)
{
    if(checked == true)
    {
        refreshTreeWidget();
    }
}
